﻿/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Core.Model;
using Core.Model.Data.Variables;
using Core.Model.Data.Type;
using ASN1Decoder.Builders.Nodes;
using Exceptions;
using Core.Model.Groups;

namespace ASN1Decoder.Builders
{
    internal class VariableBuilder
    {
        public static void Build(TaggedNode node, Variable var, Group group)
        {
            IngredientBuilder.Build(node, var, group);
        }

        internal static BooleanVariable BuildBoolean(TaggedNode node, Group group)
        {
            var ret = new BooleanVariable();
            Build(node, ret,group);

            TaggedNode e;
            if ((e = node[ASN1ContextCodes.C_ORIGINAL_VALUE]) != null)
                ret.OriginalValue = e.First.BoolValue;
            else
                throw new OpenMhegException("Excpeted Original Value");

            return ret;
        }

        internal static IntegerVariable BuildInteger(TaggedNode node, Group group)
        {
            var ret = new IntegerVariable();
            Build(node, ret, group);

            TaggedNode e;
            if ((e = node[ASN1ContextCodes.C_ORIGINAL_VALUE]) != null)
                ret.OriginalValue = e.First.IntValue;
            else
                throw new OpenMhegException("Excpeted Original Value");


            return ret;
        }

        internal static OctetStringVariable BuildOctetString(TaggedNode node, Group group)
        {
            var ret = new OctetStringVariable();
            Build(node, ret, group);

            TaggedNode e;
            if ((e = node[ASN1ContextCodes.C_ORIGINAL_VALUE]) != null)
                ret.OriginalValue = e.First.StringValue;
            else
                throw new OpenMhegException("Excpeted Original Value");


            return ret;
        }

        internal static ObjectRefVariable BuildObjectRef(TaggedNode node, Group group)
        {
            var ret = new ObjectRefVariable();
            Build(node, ret, group);

            TaggedNode e;
            if ((e = node[ASN1ContextCodes.C_ORIGINAL_VALUE][ASN1ContextCodes.C_OBJECT_REFERENCE]) != null)
                ret.OriginalValue = ObjectRefBuilder.Build(e.First, group); 
            else
                throw new OpenMhegException("Excpeted Original Value");

            return ret;
        }

        internal static ContentRefVariable BuildContentRef(TaggedNode node, Group group)
        {
            var ret = new ContentRefVariable();
            Build(node, ret, group);

            TaggedNode e;
            if ((e = node[ASN1ContextCodes.C_ORIGINAL_VALUE][ASN1ContextCodes.C_CONTENT_REFERENCE]) != null)
                 ret.OriginalValue = new ContentRef(e.First.StringValue);
            else
                throw new OpenMhegException("Excpeted Original Value");

            return ret;

        }
    }
}
